package com.woniu.new_orange.redis;

/**
 * @Program: new_orange->RedisOps
 * @Description: redis存取工具类
 * @Author: donghang
 * @Date: 2023-02-06 17:33
 * @Version： 1.0
 **/
import com.alibaba.fastjson.JSON;

import redis.clients.jedis.Jedis;

// redis存取工具类
public class RedisOps {


    // kv字符串存
    public static void set(String key,String value){
        Jedis jedis = RedisPoolUtil.getInstance();
        jedis.set(key, value);
        RedisPoolUtil.closeConn();
    }

    // kv字符串取
    public static String get(String key){
        Jedis jedis = RedisPoolUtil.getInstance();
        String value = jedis.get(key);
        RedisPoolUtil.closeConn();
        return value;
    }
    // kv对象存(java序列化方式)
    public static void setObjectSerialize(String key,Object object){
        Jedis jedis = RedisPoolUtil.getInstance();
        if(jedis==null){
            return;
        }
        jedis.set(key.getBytes(), SerializeUtil.serizlize(object));
        RedisPoolUtil.closeConn();
    }

    // kv对象取(java反序列化)
    public static Object getObjectSerialize(String key){
        Jedis jedis = RedisPoolUtil.getInstance();
        if(jedis==null){
            return null;
        }
        byte[] bytes = jedis.get(key.getBytes());
        RedisPoolUtil.closeConn();
        if(bytes==null){
            return null;
        }
        return SerializeUtil.deserialize(bytes);
    }

    // 删除key
    public static void del(String key){
        Jedis jedis = RedisPoolUtil.getInstance();
        if(jedis==null){
            return;
        }
        jedis.del(key.getBytes());
        RedisPoolUtil.closeConn();
    }


    // kv对象存(json方式)
    public static void setObjectJson(String key,Object object){
        Jedis jedis = RedisPoolUtil.getInstance();
        if(jedis==null){
            return;
        }
        jedis.set(key, JSON.toJSONString(object));
        RedisPoolUtil.closeConn();
    }
    // kv对象取(json方式)
    @SuppressWarnings({ "unchecked" })
    public static <T> Object getObjectJson(String key,Class<?> clazz){
        Jedis jedis = RedisPoolUtil.getInstance();
        if(jedis==null){
            return null;
        }
        String result= jedis.get(key);
        RedisPoolUtil.closeConn();
        if(result==null){
            return null;
        }
        T obj=(T)JSON.parseObject(result,clazz);
        return obj;
    }

    // kv对象存(map形式)
    public static void setObjectHash(String key, Object u) throws Exception {
        Jedis jedis = RedisPoolUtil.getInstance();
        if(jedis==null){
            return ;
        }
        jedis.hmset(key, MapUtil.objectToMap(u));
        RedisPoolUtil.closeConn();

    }

    // kv对象取(map形式)
    public static Object getObjectHash(String key,Class<?> clazz) throws Exception {
        Jedis jedis = RedisPoolUtil.getInstance();
        if(jedis==null){
            return null;
        }
        Object obj = MapUtil.mapToObject(jedis.hgetAll(key), clazz);
        RedisPoolUtil.closeConn();
        if(obj==null){
            return null;
        }
        return obj;
    }
}

